package com.jxbeidou.system.org.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import com.jxbeidou.core.entity.PageResult;
import com.jxbeidou.core.mybatis.service.impl.BaseServiceImpl;
import com.jxbeidou.core.mybatis.convert.PageResultConvert;
import com.jxbeidou.system.org.convert.PostConvert;
import com.jxbeidou.system.org.entity.PostDO;
import com.jxbeidou.system.org.query.PostQuery;
import com.jxbeidou.system.org.vo.PostVO;
import com.jxbeidou.system.org.mapper.PostMapper;
import com.jxbeidou.system.org.service.PostService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 岗位管理
 *
 * @author 杜晓祥 
 * @since 1.0.0 2023-12-14
 */
@Service
@AllArgsConstructor
public class PostServiceImpl extends BaseServiceImpl<PostMapper, PostDO> implements PostService {

    @Override
    public PageResult<PostVO> page(PostQuery query) {
        IPage<PostDO> page = baseMapper.selectPage(getPage(query), getWrapper(query));
        PageResult<PostVO> result = PageResultConvert.convert(page, entityList -> PostConvert.INSTANCE.convertList(entityList));
        return result;
    }

    private LambdaQueryWrapper<PostDO> getWrapper(PostQuery query){
        LambdaQueryWrapper<PostDO> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(StringUtils.isNotEmpty(query.getPostCode()), PostDO::getPostCode, query.getPostCode());
        wrapper.like(StringUtils.isNotEmpty(query.getPostName()), PostDO::getPostName, query.getPostName());
        wrapper.eq(query.getStatus() != null, PostDO::getStatus, query.getStatus());
        return wrapper;
    }

    @Override
    public void save(PostVO vo) {
        PostDO entity = PostConvert.INSTANCE.convert(vo);

        baseMapper.insert(entity);
    }

    @Override
    public void update(PostVO vo) {
        PostDO entity = PostConvert.INSTANCE.convert(vo);

        updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Long> idList) {
        removeByIds(idList);
    }

}